/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package action.retail;

import static com.opensymphony.xwork2.Action.SUCCESS;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.validator.annotations.VisitorFieldValidator;
import database.ProductDAO;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import model.Category;
import model.Product;
import model.ProductSearch;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;

/**
 *
 * @author Thai Ha Cong
 */
@Namespace("/")
public class SearchProductAction extends ActionSupport implements ModelDriven<ProductSearch> {

    private String title;
    private String description;
    private ProductSearch productSearch = new ProductSearch();
    private List<Product> productsResultList;
    private ProductDAO productDAO = new ProductDAO();
    private Map<Integer, String> allCategories;
    private List<String> allBrands;

    public SearchProductAction() {
        //
        allCategories = new HashMap<Integer, String>();
        allCategories.put(0, "");

        for (Category c : productDAO.getAllCategories()) {
            allCategories.put(c.getIdCategory(), c.getName());

            List<Category> childCategories = c.getChildCategories();
            if (childCategories != null) {
                for (Category c2 : childCategories) {
                    allCategories.put(c2.getIdCategory(), "-- " + c2.getName());
                }
            }
        }

        allBrands = new ArrayList<String>();
        allBrands.add("");

        for (String s : productDAO.getAllBrands()) {
            allBrands.add(s);
        }
    }

    public ProductSearch getProductSearch() {
        return productSearch;
    }

    public void setProductSearch(ProductSearch productSearch) {
        this.productSearch = productSearch;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public List<Product> getProductsResultList() {
        return productsResultList;
    }

    public void setProductsResultList(List<Product> productsResultList) {
        this.productsResultList = productsResultList;
    }

    public Map<Integer, String> getAllCategories() {
        return allCategories;
    }

    public void setAllCategories(Map<Integer, String> allCategories) {
        this.allCategories = allCategories;
    }

    public List<String> getAllBrands() {
        return allBrands;
    }

    public void setAllBrands(List<String> allBrands) {
        this.allBrands = allBrands;
    }

    @Override
    public String execute() throws Exception {
        return SUCCESS;
    }

    @Action(value = "advancedSearchProduct", results = {
        @Result(name = "success", location = "/home.jsp"),
        @Result(name = "input", location = "/advancedSearch.jsp")
    })
    public String advancedSearchProduct() {
        productsResultList = productDAO.searchForProductsByModel(productSearch);

        title = "Advanced Search Results";
        description = "Advanced Search Product Results";

        return SUCCESS;
    }

    @Override
    @VisitorFieldValidator(message = "", appendPrefix = false)
    public ProductSearch getModel() {
        return productSearch;
    }
}